package com.jm.offer.easy;

import java.util.Stack;

/**
 * @Description https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
 * @date 2022/4/20 8:08
 */
public class _30包含min函数的栈 {

    Stack<Integer> stack, minStack;

    public _30包含min函数的栈() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    public void push(int x) {
        stack.add(x);
        // 如果min中为空，或者小于栈顶的值也加入到min中
        if(minStack.empty() || minStack.peek() >= x) {
            minStack.add(x);
        }
    }

    public void pop() {
        // 如果弹出的值等于min栈顶元素，min也弹出
        if(stack.pop().equals(minStack.peek())) {
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return minStack.peek();
    }

}
